﻿using System;
using System.Reflection;

namespace Framework.Singleton
{
    public class Singleton<T> where T : Singleton<T>
    {
        protected Singleton()
        {
            Type type = typeof(T);
            ConstructorInfo[] ctros = type.GetConstructors();
            if (ctros.Length > 0)
            {
                string errorMessage = string.Format("{0} has at least one accesible constructor.", type.Name);

#if UNITY_EDITOR
                throw new Exception(errorMessage);
#endif
            }
        }

        private static T instance;
        /// <summary>获取实例</summary>
        public static T Instance
        {
            get
            {
                if (instance == null)
                    instance = Activator.CreateInstance(typeof(T), true) as T;

                return instance;
            }
        }
    }
}